﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Model;
using BLL;
namespace stock.GuPiao
{
    public partial class User : System.Web.UI.Page
    {
        CustomerBLL BL = new CustomerBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id"] == null)
            {
                Server.Transfer("login.aspx");
                return;
            }
            if (!Page.IsPostBack)
            {
                IniData();
            }
        }

        private void IniData()
        {
            Customer customer = BL.GetModel(int.Parse(Session["id"].ToString()));
            if (customer!=null)
            {
                lbName.Text = customer.name;
                tbNick.Text = customer.Nick;
                tbPhone.Text = customer.CellPhone;
                tbMail.Text = customer.Email;
                int state = customer.Status;
                if (state == 0)
                {
                    LinkButton1.Enabled = true;
                    LinkButton1.Visible = true;
                    hfMail.Value = customer.Email;
                }
                else
                {
                    LinkButton1.Enabled = false;
                    LinkButton1.Visible = false;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Session["id"] == null)
                {
                    Response.Redirect("login.aspx");
                }
                String oldpwd = tbOldPwd.Text.Trim();
                String newPwd = tbNewPwd.Text.Trim();
                String email = tbMail.Text.Trim();
                String nick = tbNick.Text.Trim();
                String phone = tbPhone.Text.Trim();
                oldpwd = BLL.DESEncrypt.Encrypt(oldpwd);                
                Customer user = new Customer();
                user = BL.GetModel(int.Parse(Session["id"].ToString()));
                if (oldpwd == user.Password)
                {
                    newPwd = BLL.DESEncrypt.Encrypt(newPwd);
                    user.Nick = nick;
                    user.Password = newPwd;
                    user.CellPhone = phone;
                    user.Email = email;
                    string script = "";
                    if (BL.Update(user))
                    {
                        script = "<script language='Javascript'>alert('更新成功！');</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        return;
                    }
                    else
                    {
                        script = "<script language='Javascript'>alert('更新失败！');</script>";
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
                        return;
                    }
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string code = Tools.GetRandomCode(5);
            String body = "telwo,激活码为："+code;
            Session["codes"] = code;
            Tools.SendMail(hfMail.Value, body, "谢谢您使用",null, false);
            string script = "<script language='Javascript'>alert('激活码已经发送到信箱，注意查收！');location.href='active.aspx';</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", script);
            return;
        }
    }
}